package com.aaa.mrcai.controller;

import com.aaa.mrcai.pojo.entity.BussinessInfo;

import com.aaa.mrcai.pojo.vo.BusinessIntoVO;
import com.aaa.mrcai.pojo.vo.BusinessPicVO;
import com.aaa.mrcai.pojo.vo.IdcardVO;
import com.aaa.mrcai.properties.OSSProperties;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.result.IDCardResult;
import com.aaa.mrcai.service.BusinessRegisterService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

/**
 * @ Author: Lining
 * @ Date: 2021/12/10 12:06
 */

@RestController
@RequestMapping("businessRegister")
public class BusinessRegisterController {

    @Autowired
    private BusinessRegisterService businessRegisterService;

    @Autowired
    private OSSProperties ossProperties;

    @RequestMapping("certification")
    public IDCardResult certification(IdcardVO idcardVO){

        String url = "https://eid.shumaidata.com/eid/check";
        String appCode = "ded33b65a23b44178ba2ecac60686713";
        System.out.println("idcard:"+idcardVO.getIdcard());
        System.out.println("bname:"+idcardVO.getBname());
        Map<String, String> params = new HashMap<>();
        params.put("idcard", idcardVO.getIdcard());
        params.put("name", idcardVO.getBname());
        System.out.println("params:"+params);
        OkHttpClient client = new OkHttpClient.Builder().build();
        FormBody.Builder formbuilder = new FormBody.Builder();
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            formbuilder.add(key, params.get(key));
        }
        FormBody body = formbuilder.build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(body).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("返回状态码" + response.code() + ",message:" + response.message());
        String result=null;
        try {
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("result"+result);
        IDCardResult idCardResult = JSON.toJavaObject(JSONObject.parseObject(result), IDCardResult.class);
        System.out.println("idCardResult"+idCardResult);
        return idCardResult;
    }

    @RequestMapping("addInfo")
    public AjaxResult addInfo(BussinessInfo bussinessInfo){
        return businessRegisterService.addInfo(bussinessInfo);
    }

    @RequestMapping("addStore")
        public AjaxResult addStore(BusinessIntoVO businessIntoVO){
        return businessRegisterService.addStore(businessIntoVO);
    }

    @RequestMapping("getState")
    public AjaxResult getState(BussinessInfo bussinessInfo){
        return businessRegisterService.getState(bussinessInfo);
    }


    @RequestMapping("addPic1")
    /*pic1 与前端传过来的input框中的name一样*/

    public AjaxResult addPic1(BusinessPicVO businessPicVO,MultipartFile pic1) throws IOException {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());

        // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        long time = System.currentTimeMillis();
        String imgName= UUID.randomUUID().toString()+time+pic1.getContentType().replace("image/",".");

        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(ossProperties.getBucket(), imgName, pic1.getInputStream());
        // 关闭OSSClient。
        ossClient.shutdown();
        //真实路径
        businessPicVO.setBicon(ossProperties.getRealUrl()+imgName);
        return  businessRegisterService.addPic1(businessPicVO);
    }

    @RequestMapping("addPic2")
    /*pic1 与前端传过来的input框中的name一样*/

    public AjaxResult addPic2(BusinessPicVO businessPicVO,MultipartFile pic2) throws IOException {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());

        // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        long time = System.currentTimeMillis();
        String imgName= UUID.randomUUID().toString()+time+pic2.getContentType().replace("image/",".");

        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(ossProperties.getBucket(), imgName, pic2.getInputStream());
        // 关闭OSSClient。
        ossClient.shutdown();
        //真实路径
        businessPicVO.setLicense(ossProperties.getRealUrl()+imgName);
        return  businessRegisterService.addPic2(businessPicVO);
    }

    @RequestMapping("addPic3")
    /*pic1 与前端传过来的input框中的name一样*/

    public AjaxResult addPic3(BusinessPicVO businessPicVO,MultipartFile pic3) throws IOException {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());

        // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        long time = System.currentTimeMillis();
        String imgName= UUID.randomUUID().toString()+time+pic3.getContentType().replace("image/",".");

        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(ossProperties.getBucket(), imgName, pic3.getInputStream());
        // 关闭OSSClient。
        ossClient.shutdown();
        //真实路径
        businessPicVO.setCardfront(ossProperties.getRealUrl()+imgName);
        return  businessRegisterService.addPic3(businessPicVO);
    }

    @RequestMapping("addPic4")
    /*pic1 与前端传过来的input框中的name一样*/

    public AjaxResult addPic4(BusinessPicVO businessPicVO,MultipartFile pic4) throws IOException {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());

        // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        long time = System.currentTimeMillis();
        String imgName= UUID.randomUUID().toString()+time+pic4.getContentType().replace("image/",".");

        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(ossProperties.getBucket(), imgName, pic4.getInputStream());
        // 关闭OSSClient。
        ossClient.shutdown();
        //真实路径
        businessPicVO.setCardreverse(ossProperties.getRealUrl()+imgName);
        return  businessRegisterService.addPic4(businessPicVO);
    }

}
